$(document).ready(function() {
	
	function mostrarOp(idTR, s) {
		if(s)
			$("#"+idTR+" .edit, #"+idTR+" .delete").css("display", "block");
		else
			$("#"+idTR+" .edit, #"+idTR+" .delete").css("display", "none");
	}
	
	function carregarFuncoes() {
		
		$(".tabelaDefault tbody tr").hover(function() {
			mostrarOp(this.id, true); //in
		}, function() {
			mostrarOp(this.id, false); //out
		});
		
		$(".tabelaDefault tbody td").click(function() {
			
			var id = $(this).parent().attr("id");
			var edit = $(this).hasClass("edit");
			var del = $(this).hasClass("delete");
			
			if(edit)
				editar(id);
			else if(del)
				deletar(id);
		});
	}
	
	function montarTabela(j) {
		var linha;
		var dataAtual = "";
		var hoje = new Date();
		var x;
		var d;
		var diaDaSemana = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];
		$("#homeLogadoContainer .conteudo-top").html("<table align='left' class='tabelaDefault'><tbody></tbody></table>");
	    
		for(i = 0; i < j.length; i++) {
			if(dataAtual != j[i].data){
				dataAtual = j[i].data;
				d = dataAtual.split("/");
				d = new Date(d[2],(d[1]-1),d[0]);
				x = (hoje.getDate() == d.getDate()) && (hoje.getMonth() == d.getMonth()) && (hoje.getFullYear() == d.getFullYear());
				linha = "<tr class='dia-da-semana'><td colspan='5'>"+((x)?"Hoje, ":diaDaSemana[d.getDay()]+", ")+ dataAtual +"</td></tr>";
				$("#homeLogadoContainer .conteudo-top table tbody").append(linha);
			}
			linha = (i%2==0?
					"<tr id='"+j[i].id_c+"' class='linhaA'><td>"+j[i].horario+"</td><td class='tam4'>"+(j[i].id_p==0?"sem paciente":j[i].id_p)+"</td><td class='tam3'>"+j[i].descricao+"</td><td title='editar' class='edit'></td><td title='remover' class='delete'></td></tr>":
					"<tr id='"+j[i].id_c+"' class='linhaB'><td>"+j[i].horario+"</td><td class='tam4'>"+(j[i].id_p==0?"sem paciente":j[i].id_p)+"</td><td class='tam3'>"+j[i].descricao+"</td><td title='editar' class='edit'></td><td title='remover' class='delete'></td></tr>");
	    	$("#homeLogadoContainer .conteudo-top table tbody").append(linha);
	    }
	}
	
	function listar() { //via GET
		$.ajax({
			type: "GET",
			url: "/Fisio-web/agenda",
			data: {
				op: "listar"
			},
	    	success: function(jsonArray) {
				montarTabela(jsonArray);
				carregarFuncoes();
	    	}
		});
	}
	
	function listarComFiltro(tipo) { //via GET
		$.ajax({
			type: "GET",
			url: "/Fisio-web/agenda",
			data: {
				op: "listar",
				tipo: tipo,
				dataInicio: $("#dataInicio").val(),
				dataFim: $("#dataFim").val()
			},
	    	success: function(jsonArray) {
				montarTabela(jsonArray);
				carregarFuncoes();
	    	}
		});
	}
	
	function deletar(id) { //via POST
		$.ajax({
			type: "POST",
			url: "/Fisio-web/agenda",
			data: {
				op: "deletar",
				id: id
			},
	    	success: function() {
				$(".tabelaDefault #"+id).remove();
	    	}
		});
	}
	
	function editar(id) {
		location.href = "/Fisio-web/agenda?op=editar&id="+id; //via GET
	}
	
	$("#tabAgenda").click(function() {
		listar();
	});
	
	$("#btnHoje").click(function() {
		listarComFiltro("hoje");
	});
	
	$("#btnSemana").click(function() {
		listarComFiltro("semana");
	});
	
	$("#btnMes").click(function() {
		listarComFiltro("mes");
	});
	
	$("#pesquisarCompromissos").click(function() {
		listarComFiltro("periodo");
	});
	
	/* DINAMISMO */
	if($.tabAtiva == "#tabAgenda")
		listar();
});